# coding:utf-8
from ctypes import cdll, c_float, c_double, c_bool, c_uint, c_int, c_char_p, Array


class MyTemplateNative:
    dylib = None

    def __init__(self, path):
        print("class MyTemplateNative method __init__")
        print("[dylibPath]:" + path)
        self.dylib = cdll.LoadLibrary(path)
        pass

    # basic_type
    def basic_integer(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.basic_integer
        native_function.argtypes = [c_int]
        native_function.restype = c_int

        return native_function(value)
        pass

    # 这里存在问题，转换后为long型
    def basic_uinteger(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.basic_uinteger
        native_function.argtypes = [c_uint]
        native_function.restype = c_uint

        return native_function(value)
        pass

    def basic_float(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.basic_float
        native_function.argtypes = [c_float]
        native_function.restype = c_float

        return native_function(value)
        pass

    def basic_doube(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.basic_doube
        native_function.argtypes = [c_double]
        native_function.restype = c_double

        return native_function(value)
        pass

    def basic_bool(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.basic_bool
        native_function.argtypes = [c_bool]
        native_function.restype = c_bool

        return native_function(value)
        pass

    def basic_str(self, value):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        if isinstance(value, str) is False:
            raise Exception("args value is not str type.")

        native_function = self.dylib.basic_str
        native_function.argtypes = [c_char_p]
        native_function.restype = c_char_p

        result = native_function(value.encode('utf-8'))
        return result.decode('utf-8')
        pass

    def inject_function(self, function):
        print(function)
        function()
        pass

    # compound type
    def array_12345(self):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.array_12345
        native_function.restype = Array
        return native_function()


    def array_months(self):
        if self.dylib is None:
            raise Exception("dylib cannot find.")

        native_function = self.dylib.array_months
        # https://geek-docs.com/python/python-ask-answer/225_python_pointers_and_arrays_in_python_ctypes.html
#         native_function.restype = c_int
        return native_function()
